{% extends 'base.html' %}
{% load static %}

{% block title %}资产日志详情 - 固定资产管理系统{% endblock %}

{% block content %}
<div class="container py-3">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h3><i class="fas fa-file-alt"></i> 日志详情</h3>
    <div>
      <a class="btn btn-outline-secondary" href="{% url 'assets:asset_log_list' %}"><i class="fas fa-arrow-left"></i> 返回列表</a>
      {% if perms.assets.delete_assetlog %}
      <form method="post" action="{% url 'assets:asset_log_delete' log.id %}" class="d-inline-block ms-2" onsubmit="return confirm('确定删除该日志记录吗？此操作不可恢复。');">
        {% csrf_token %}
        <button type="submit" class="btn btn-outline-danger"><i class="fas fa-trash"></i> 删除</button>
      </form>
      {% endif %}
    </div>
  </div>

  <div class="card">
    <div class="card-body">
      <div class="row g-3">
        <div class="col-md-6">
          <label class="text-muted">时间</label>
          <div>{{ log.created_at|date:"Y-m-d H:i:s" }}</div>
        </div>
        <div class="col-md-6">
          <label class="text-muted">操作</label>
          <div><span class="badge bg-secondary">{{ log.get_action_display }}</span></div>
        </div>
        <div class="col-md-6">
          <label class="text-muted">资产</label>
          <div>
            {% if log.asset %}
              <a href="{% url 'assets:asset_detail' log.asset_id %}">{{ log.asset.name }} ({{ log.asset.asset_number }})</a>
            {% else %}
              <span class="text-muted">-</span>
            {% endif %}
          </div>
        </div>
        <div class="col-md-6">
          <label class="text-muted">操作人</label>
          <div>{% if log.operator %}{{ log.operator.username }}{% else %}<span class="text-muted">系统</span>{% endif %}</div>
        </div>
        <div class="col-12">
          <label class="text-muted">描述</label>
          <div>{{ log.description|default:'-' }}</div>
        </div>
        {% with pairs=log.get_change_pairs %}
          {% if pairs %}
          <div class="col-12">
            <label class="text-muted">变更内容</label>
            <div class="table-responsive">
              <table class="table table-sm table-bordered align-middle mb-0">
                <thead class="table-light">
                  <tr>
                    <th style="width: 220px;">字段</th>
                    <th>旧值</th>
                    <th>新值</th>
                  </tr>
                </thead>
                <tbody>
                  {% for item in pairs %}
                  <tr>
                    <td>{{ item.field }}</td>
                    <td><code class="text-muted">{{ item.old }}</code></td>
                    <td><code>{{ item.new }}</code></td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            </div>
          </div>
          {% elif log.old_value or log.new_value %}
          <div class="col-md-6">
            <label class="text-muted">旧值</label>
            <pre class="mb-0"><code>{{ log.old_value|default:'-' }}</code></pre>
          </div>
          <div class="col-md-6">
            <label class="text-muted">新值</label>
            <pre class="mb-0"><code>{{ log.new_value|default:'-' }}</code></pre>
          </div>
          {% endif %}
        {% endwith %}
      </div>
    </div>
  </div>
</div>
{% endblock %}
